from urllib import request
import mysql

head={
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36"
}


funds = []
fundNum = 0
errorNum = 0
send = request.Request("http://fund.eastmoney.com/js/fundcode_search.js",headers = head)
response = request.urlopen(send)
js = response.read().decode('utf-8')
js = js[11:len(js)-3].split("],[")
for i in range(0,len(js)):
    fund = str(js[i]).replace('"','')
    fund = fund.split(",")

    # if fund[3] == "混合型" or fund[3] == "股票型" or fund[3] == "股票指数":
    funds.append(fund)

mysql.initFundTable(funds)

print("程序运行结束")


















# fundFile = open('Fund.txt', 'r')
# fundData = fundFile.read()
# # data1 = fundData.replace("[", "").replace("]", "").replace("\"", "")
# arr = fundData.split('],[')

# db = MySQLdb.connect("localhost", "jijin", "jijin", "jijin", charset='utf8' )

# cursor = db.cursor()

# for items in arr:

#     subItem = items.replace("\"", "").split(',')
#     print(subItem[0])

#     sql = "INSERT INTO fund (fundId, fundName, type1) VALUES (%s, %s, %s)"
#     par = (subItem[0], subItem[2], subItem[3])

    
#     try:
#         cursor.execute(sql, par)
#         db.commit()
#     except:
#         db.rollback()

# db.close()

# print("程序运行结束")

# fundFile.close()